//
//  SISSTenantTakeLookDetailViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/26.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTenantTakeLookDetailViewController.h"

#import "SISSNetworkingManager+Progress.h"
#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSTenantProgressModel.h"
#import "SISSShowView.h"
#import "SISSRentOutView.h"
#import "SISSTenantTakeLookModel.h"
#import "SISSCommentModel.h"

@interface SISSTenantTakeLookDetailViewController ()

/* 出租信息View */
@property (strong, nonatomic) UIView *takeLookRentOutInfoView;
@property (weak, nonatomic) SISSShowView *takeLookTimeView;
@property (strong, nonatomic) SISSRentOutView *rentOutInfoView;

/* 带看详情模型 */
@property (strong, nonatomic) SISSTenantTakeLookModel *takeLookDetailModel;

@end

@implementation SISSTenantTakeLookDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setTakeLookDetailModel:(SISSTenantTakeLookModel *)takeLookDetailModel {
    _takeLookDetailModel = takeLookDetailModel;
    
    //    NSMutableArray *imagesURLStrings = [NSMutableArray arrayWithCapacity:takeLookDetailModel.takeLookPic.count];
    //    for (SISSTakeLookPicModel *model in takeLookDetailModel.takeLookPic) {
    //        [imagesURLStrings addObject:model.picPath];
    //    }
    
    NSArray *imagesURLStrings = [NSArray arrayWithArray:takeLookDetailModel.image];
    
    self.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    self.imagesURLStrings = imagesURLStrings.copy;
    NSInteger index = takeLookDetailModel.image.count > 0 ? 1 : 0;
    self.pageLabel.text = [NSString stringWithFormat:@"%zd/%zd",index,self.imagesURLStrings.count];
    
    
    self.rentOutInfoView.rentOutModel = takeLookDetailModel.lease;
    self.takeLookTimeView.rightStr = [NSDate yyr_stringToString:takeLookDetailModel.takelookDate];
}

- (void)setTakeLookModel:(SISSTenantTakeLookModel *)takeLookModel {
    _takeLookModel = takeLookModel;
    self.brokerState = takeLookModel.landlordState == 1 && takeLookModel.renterState == 1;
}

- (void)setTenantProgressModel:(SISSTenantProgressModel *)tenantProgressModel {
    _tenantProgressModel = tenantProgressModel;
    self.brokerState = self.takeLookModel.landlordState == 1 && self.takeLookModel.renterState == 1;
    self.infoView = self.takeLookRentOutInfoView;
}

- (void)bottomButtonAssinment {
    
    self.bottomButton.hidden = NO;
    self.trueButton.hidden = YES;
    self.untrueButton.hidden = YES;
    
    NSString *buttonStr;
    if (self.takeLookModel.leaseType == 1) {
        
        if (self.takeLookModel.landlordState == 0 && self.takeLookModel.renterState == 0) {
            buttonStr = @"等待确认";
        } else if (self.takeLookModel.landlordState == 2 || self.takeLookModel.renterState == 2) {
            buttonStr = self.takeLookModel.landlordState == 2 ? @"出租方否认看房" : @"承租方否认看房";
        } else if (self.takeLookModel.landlordState == 1 && self.takeLookModel.renterState == 1) {
            buttonStr = @"评价";
        } else if (self.takeLookModel.landlordState == 1 || self.takeLookModel.renterState == 1) {
            buttonStr = self.takeLookModel.landlordState == 1 ? @"等待出租方确认" : @"等待承租方确认";
        }
        self.bottomButton.userInteractionEnabled = self.takeLookModel.landlordState == 1 && self.takeLookModel.renterState == 1;
    } else {
        if (self.takeLookModel.renterState == 0) {
            buttonStr = @"等待确认";
        } else if (self.takeLookModel.renterState == 2) {
            buttonStr = @"对方否认看房";
        } else if (self.takeLookModel.renterState == 1) {
            buttonStr = @"评价";
        }
        self.bottomButton.userInteractionEnabled = self.takeLookModel.renterState == 1;
    }

    
    [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
}

/**
 删除带看
 */
- (void)deleteTakeLook {
    
    [kNetworkManager deleteLeaseTakelookWithId:self.takeLookModel.ltlrId success:^(id responseObject) {
        
        if (self.deleteCompletedBlock) {
            self.deleteCompletedBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


/**
 加载评论数据
 */
- (void)requestCommentData {
    
        WeakSelf
    [kNetworkManager requestCommentTakeLookTenantCommentBrokerInfoWithId:self.takeLookModel.ltlrId success:^(SISSCommentModel *commentModel) {
        
        weakSelf.beCommentedView.hidden = !commentModel;
        weakSelf.beAcrId = commentModel.ID;
        weakSelf.beCommentedView.commentModel = commentModel;
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    [kNetworkManager requestCommentTakeLookBrokerCommentTenantInfoWithId:self.takeLookModel.ltlrId success:^(SISSCommentModel *commentModel) {
        
        weakSelf.toCommentShowView.hidden = !commentModel;
        weakSelf.toCommentShowView.commentModel = commentModel;
        NSString *buttonStr;
        if (commentModel.lstOfAdditional.firstObject) {
            buttonStr = @"已评价";
            weakSelf.bottomButton.userInteractionEnabled = NO;
        } else if (commentModel) {
            weakSelf.toAcrId = commentModel.ID;
            weakSelf.isAdditionalComment = YES;
            buttonStr = @"追加";
        }
        if (commentModel) [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

/**
 带看详情
 */
- (void)requestTakeLookDetailData {
    
    WeakSelf
    [kNetworkManager requestTenantTakelookDetailWithLtlrId:self.takeLookModel.ltlrId success:^(SISSTenantTakeLookModel *takeLookDetailModel) {
    
            weakSelf.takeLookDetailModel = takeLookDetailModel;

        } failed:^(NSError *error) {

            NSLog(@"%@",error);
        }];
}

/**
 评论
 */
- (void)addComment {
    
    WeakSelf
    //    NSString *content = [NSString unicodeToISO88591:self.midView.commentTextView.text];
    NSString *content = self.midView.commentTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    NSDictionary *parameter = @{
                                @"spId":[SISSUserAccount shared].spId,
                                @"ltlrId":self.takeLookModel.ltlrId,
                                @"arId":self.tenantProgressModel.arId,
                                @"arbpId":self.tenantProgressModel.arbpId,
                                @"score":@(self.midView.gradeStar),
                                @"content":content,
                                @"comment_type":@(1)
                                };
    
    [kNetworkManager insertBrokerCommentTenantWithParameters:parameter success:^(id responseObject) {
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

/**
 追加
 */
- (void)addAdditionalComment {
    NSLog(@"%@",self.additonalTextView.text);
    WeakSelf
    //    NSString *content = [NSString unicodeToISO88591:self.additonalTextView.text];
    NSString *content = self.additonalTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    [kNetworkManager insertAdditionalCommentWithAcrId:self.toAcrId content:content commentType:10 success:^(id responseObject) {
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - 懒加载
- (SISSRentOutView *)rentOutInfoView {
    if (!_rentOutInfoView) {
        _rentOutInfoView = [[SISSRentOutView alloc] init];
    }
    return _rentOutInfoView;
}

- (UIView *)takeLookRentOutInfoView {
    if (!_takeLookRentOutInfoView) {
        _takeLookRentOutInfoView = [[UIView alloc] init];
        
        [_takeLookRentOutInfoView addSubview:self.rentOutInfoView];
        
        SISSShowView *takeLookTimeView = [[SISSShowView alloc] init];
        takeLookTimeView.leftStr = @"看房时间：";
        takeLookTimeView.rightStr = @"";
        [_takeLookRentOutInfoView addSubview:takeLookTimeView];
        self.takeLookTimeView = takeLookTimeView;
        
        [self.rentOutInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self->_takeLookRentOutInfoView);
        }];
        
        [self.takeLookTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.rentOutInfoView.mas_bottom);
            make.left.right.bottom.equalTo(self->_takeLookRentOutInfoView);
            make.height.mas_equalTo(40);
        }];
        
    }
    return _takeLookRentOutInfoView;
}



@end
